package mo.tags.greedy;

import java.util.Arrays;

public class L948 {

    public int bagOfTokensScore(int[] tokens, int power) {
        Arrays.sort(tokens);
        if (tokens.length == 0 || power < tokens[0]) {
            return 0;
        }
        int left = 0, right = tokens.length - 1;
        int score = 0;
        int max = 0;
        while (left <= right) {
            if (power >= tokens[left]) {
                score++;
                max = Math.max(max, score);
                power -= tokens[left];
                left++;
            } else {
                score--;
                power += tokens[right];
                right--;
            }
        }
        return max;
    }

    public static void main(String[] args) {
        String prefix = "密封环 N50-9-1".substring(0, "密封环 N50-9-1".lastIndexOf('-') + 1) + "%";
        System.out.println(prefix);
        L948 test = new L948();
        System.out.println(test.bagOfTokensScore(new int[]{100,200}, 150));
    }
}
